package cn.xuanxuanyuhua.crm.service.impl;


import cn.xuanxuanyuhua.crm.domain.SystemDictionary;
import cn.xuanxuanyuhua.crm.domain.SystemDictionaryItem;
import cn.xuanxuanyuhua.crm.mapper.SystemDictionaryItemMapper;
import cn.xuanxuanyuhua.crm.mapper.SystemDictionaryMapper;
import cn.xuanxuanyuhua.crm.query.QueryObject;
import cn.xuanxuanyuhua.crm.service.ISystemDictionaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemDictionaryServiceImpl implements ISystemDictionaryService {
    @Autowired
    private SystemDictionaryMapper dictDAO;
    @Autowired
    private SystemDictionaryItemMapper itemDAO;

 

    @Override
    public PageInfo querySystemDictionary(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<SystemDictionary> list = dictDAO.selectAll();
        return new PageInfo(list);
    }

    @Override
    public PageInfo querySystemDictionaryItem(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<SystemDictionaryItem> list = itemDAO.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public List<SystemDictionaryItem> selectAllBySn(String job) {
        return itemDAO.selectAllBySn(job);
    }

    @Override
    public void saveOrUpdate(SystemDictionary entity) {
        if (entity.getId() != null) {
            dictDAO.updateByPrimaryKey(entity);
        }else{
            dictDAO.insert(entity);
        }
    }

    @Override
    public void saveOrUpdate(SystemDictionaryItem entity) {
        if (entity.getId() != null) {
            itemDAO.updateByPrimaryKey(entity);
        }else{
            Integer maxSequence = itemDAO.selectMaxSequenceByParentId(entity.getParentId());
            if (maxSequence == null) {
                maxSequence = 0;
            }
            entity.setSequence(++maxSequence);
            itemDAO.insert(entity);
        }
    }

    @Override
    public void deleteSystemDictionary(Long id) {
        itemDAO.deleteByParentId(id);
        dictDAO.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteSystemDictionaryItem(Long id) {
        itemDAO.deleteByPrimaryKey(id);
    }

    @Override
    public SystemDictionary get(Long id) {
        return dictDAO.selectByPrimaryKey(id);
    }

    @Override
    public List<SystemDictionary> list() {

        return dictDAO.selectAll();
    }
}
